/*
#Copyright (c) 2013, Philippe Bordron <philippe.bordron@gmail.com>
#
# This file is part of SIPPER.
#
# DCC is free software: you can redistribute it and/or modify
# it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# DCC is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU LESSER GENERAL PUBLIC LICENSE for more details.
#
# You should have received a copy of the GNU LESSER GENERAL PUBLIC LICENSE
# along with SIPPER.  If not, see <http://www.gnu.org/licenses/>
*/
package combi.wip.sipper.exec;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import combi.wip.graph.algo.ksp.LabelGroupeFromMap;
import combi.wip.graph.algo.ksp.Ksp_Yen_P2P_mt;

public class ComputeSIPs_old {

	private static int nbParametresObligatoires = 6;



	/*
	 * starts sous la forme suivante
	 * 'label A'\t'sommet1' 'sommet2' ...
	 */

	/**
	 * @param args
	 * g : Graph
	 * starts : sources vertices
	 * ends : destination vertices
	 * k : number of paths between each source vertex in starts and each destination vertex in ends
	 * out : output file
	 * labels : vertices names
	 * nb_threads : number of threads
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		if (args.length < nbParametresObligatoires)
		{
			System.err.println("Incorrect parameters: parameters must be \n" +
					"g starts ends k out nb_threads\n"+
					"g : Graph\n" +
					"starts : list of named set of sources vertices\n" +
					"ends : list of named set of destination vertices\n" +
					"k : number of paths from each set of source verices in starts to each set of destination vertices\n" +
					"out : output file\n" +
					//"labels : vertices names\n" +
					"nb_threads : number of threads");
			System.exit(1);
		}

		String graphFile = args[0];
		String sourcesFile = args[1];
		String destFile = args[2];
		int k = new Integer(args[3]);
		String fileOut = args[4];
		//String nomGroupesFile = args[5];
		int	nbThreads = new Integer(args[5]);

		Map<String, Set<Integer>> deb;
		try {
			deb = loadAsso(sourcesFile, "(?:(.+)\\t(.+))");
		Map<String, Set<Integer>> fin = loadAsso(destFile, "(?:(.+)\\t(.+))");
		Map<String, Set<Integer>> nomGroupes = new HashMap<String, Set<Integer>>(deb);
		nomGroupes.putAll(fin);
		//Map<String, Set<Integer>> nomGroupes = loadAsso(nomGroupesFile, "(?:(.+)\\t(.+))");
		Ksp_Yen_P2P_mt algo = new Ksp_Yen_P2P_mt(graphFile, deb.values(), fin.values(), k, fileOut);
		Ksp_Yen_P2P_mt.set_NB_THREADS(nbThreads+1);
		algo.setLabelsGroupes(new LabelGroupeFromMap<Set<Integer>>(inverseMap(nomGroupes)));
		algo.run();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// Il doit y avoir une bijection entre les deux partie de la map.
	private static Map<Set<Integer>, String> inverseMap(Map<String, Set<Integer>> map)
	{
		Map<Set<Integer>, String> newMap = new HashMap<Set<Integer>, String>();
		for (Entry<String, Set<Integer>> entry : map.entrySet())
		{
			String old = newMap.put(entry.getValue(), entry.getKey());
			if(old != null)
			{
				System.err.println(old);
			}
		}
		return newMap;
	}
	
	public static Map<String, Set<Integer>> loadAsso(String fileIn, String tag) throws IOException
	{
		Map<String, Set<Integer>> result = new HashMap<String, Set<Integer>>();
		BufferedReader in = new BufferedReader (new FileReader (fileIn));
		Matcher m;
		Matcher m2;
		String nb = "(?:([0-9]+))";
		Pattern reco = Pattern.compile(tag);
		Pattern recoNb = Pattern.compile(nb);
		String ch;
		while ((ch = in.readLine()) != null)
		{
			try
			{
				m = reco.matcher(ch);
				while (m.find())
				{
					Set<Integer> set = new HashSet<Integer>();
					result.put(m.group(1), set);
					m2 = recoNb.matcher(m.group(2));
					while (m2.find())
					{
						set.add(Integer.valueOf(m2.group(1)));
					}
				}
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}
		in.close();
		return result;
	}
}